#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# @Time: 2019/7/18  18:23
# @Author: 余浪人
# @email: yulangren520@gmail.com
from fake_useragent import UserAgent
import requests
from flask import url_for

from apps.lib.cache_sys import current_theme


def status(statu: int):
    if statu == 1:
        return '已启用'
    elif statu == 0:
        return '未启用'
    elif statu == -1:
        return '预删除'


def property(statu):
    '''
    数据属性
    :param statu:
    :return:
    '''
    if statu is 1:
        return '是'
    elif statu:
        return '<font style="color:#FF5722">是</font>'
    elif not statu:
        return '否'


def is_pay(status: int):
    res = '付费可见' if status == 1 else '免费可见'
    return res


def pro_column_name(statu: int):
    if statu == 0: return '顶级分类'
    from apps.models.content_model import TL_Column_model
    col = TL_Column_model.query.filter_by(id=statu).first()
    if col is None: return '识别失败!'
    return col.column_name


def ipAddress(ip: str):
    userAgent = UserAgent()
    url = f'https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query={ip}&resource_id=6006'
    headers = {"User-Agent": userAgent.random}
    try:
        response = requests.get(url, headers=headers, timeout=0.1)
        if response.status_code == 200:
            json_data = response.json().get('data')[0]
            address = json_data.get('location')
            data = address
        else:
            data = '未知地区'
    except:
        data = '未知地区'
    return data

def static(filename):
    '''
    主题模板静态资源
    :param filename: 资源名称
    :return:
    '''
    theme = current_theme()
    return url_for('static', filename=f'web_themes/{theme}/{filename}')

def install_status(statu):
    '''
    数据属性
    :param statu:
    :return:
    '''
    if statu is 1:
        return '已安装'
    return '未安装'